;$(function () {

    appendImg();

    $(".form-table").on("click", ".download-a", function () {
        var attachmentId = $(this).data("id");
        downloadImg(attachmentId);
    });
    $(".form-table").on("click", ".delete-a", function () {
        var attachmentId = $(this).data("id");
        deleteImg(attachmentId);
    });

    function uploadImg(url, signature) {
        var baseDownloadUrl = baseDomain + "/attachment/";
        var upload = layui.upload;
        var uploadInst = upload.render({
            elem: '#' + signature + '' //绑定元素
            , url: url //上传接口
            , accept: 'images'
            , multiple: true
            , done: function (res) {
                //上传完毕回调
                var $tr = $("#" + signature + "").parent();
                for (var key = 0; key < res.length; key++) {
                    var val = res[key];
                    var html =
                        '<div><hr><img alt="" src="' + baseDownloadUrl + val.downloadUrl + '">' +
                        '<a href="javascript:void(0);" data-id="' + val.attachmentId + '" class="download-a" title="' + val.fileName + '" style="display:inline-block;padding: 3px;background-color: #FFE7B7;color: #444;margin:2px;">' + val.fileName + '</a>' +
                        '<a href="javascript:void(0);" data-id="' + val.attachmentId + '" class="delete-a"><i class="fa fa fa-trash-o icon-gray"></i><font>删除<font></a>&nbsp;&nbsp;&nbsp;&nbsp' +
                        '</div>';
                    $tr.append(html);
                }
                if (res.length > 0) {
                    layer.msg("上传成功");
                }
            }
            , error: function () {
                //请求异常回调
                layer.msg('上传失败');
            }
        });
    }

    layui.use('upload', function () {

        //执行实例
        var quesId = $("input[name='question.id']").val();

        var quesAreaId = quesId + "-QUESTION";
        var quesUrl = basePath + "/attachment/uploadByBoxId?areaId=" + quesAreaId;
        var quesSig = "questionUpload";
        uploadImg(quesUrl, quesSig);

        var analysisAreaId = quesId + "-ANALYSIS";
        var analysisUrl = basePath + "/attachment/uploadByBoxId?areaId=" + analysisAreaId;
        var analysisSig = "analysisUpload";
        uploadImg(analysisUrl, analysisSig);

    });

    function appendImgByArea(areaId, $tr) {
        var attachUrl = basePath + "/attachment";
        var baseDownloadUrl = baseDomain + "/attachment/";
        $.ajax({
            url: attachUrl + "/getAttachmentsByBoxId.json?areaId=" + areaId,
            type: "get",
            dataType: "json",
            success: function (r) {
                for (var key = 0; key < r.length; key++) {
                    var val = r[key];
                    var html =
                        '<div><hr><img alt="" src="' + baseDownloadUrl + val.downloadUrl + '">' +
                        '<a href="javascript:void(0);" data-id="' + val.attachmentId + '" class="download-a" title="' + val.fileName + '" style="display:inline-block;padding: 3px;background-color: #FFE7B7;color: #444;margin:2px;">' + val.fileName + '</a>' +
                        '<a href="javascript:void(0);" data-id="' + val.attachmentId + '" class="delete-a"><i class="fa fa fa-trash-o icon-gray"></i><font>删除<font></a>&nbsp;&nbsp;&nbsp;&nbsp' +
                        '</div>';
                    $tr.append(html);
                }
            }
        });
    }

    function appendImg() {
        var quesId = $("input[name='question.id']").val();

        var quesAreaId = quesId + "-QUESTION";
        var $tr = $("#questionUpload").parent();
        appendImgByArea(quesAreaId, $tr);

        var analysisAreaId = quesId + "-ANALYSIS";
        var $tr = $("#analysisUpload").parent();
        appendImgByArea(analysisAreaId, $tr);

    }

    function deleteImg(attachmentId) {
        var url = basePath + "/attachment/deleteAttachment.json?attachmentId=" + attachmentId;
        layer.confirm("确认要删除吗，删除后不能恢复", {title: "删除确认"}, function (index) {
            layer.close(index);
            $.post(url, {}, function (data) {
                $("a[data-id='" + attachmentId + "']").parent().remove();
                layer.msg("删除成功");
            }).error(function () {
                layer.msg("删除失败，请联系管理员");
            });
        });
    }

    function downloadImg(attachmentId) {
        var url = basePath + "/attachment/download.json?address=" + attachmentId;
        var downloadIframe = document.getElementById('downLoadIframe');
        if (downloadIframe) {
            downloadIframe.src = url;
        } else {
            downloadIframe = document.createElement('iframe');
            downloadIframe.id = 'downLoadIframe';
            downloadIframe.src = url;
            downloadIframe.style.display = "none";
            document.body.appendChild(downloadIframe);
        }
    }
});